package com.hsy.sunflowerbao.utils

import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.os.Handler
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.hsy.sunflowerbao.Application
import com.hsy.sunflowerbao.R
import com.hsy.sunflowerbao.base.Config

import com.hsy.sunflowerbao.base.ui.widget.ProgressDialog
import com.hsy.sunflowerbao.login.ui.LoginActivity
import android.content.pm.PackageManager


/**
 * 通用工具
 * Created on 2017/8/28.
 *
 * @author Yang
 */
object CommonUtils {

    private const val KEY_TOKEN = "key_token"
    private const val KEY_USER_ID = "KEY_USER_ID"
    private const val KEY_SID = "KEY_sid"
    private const val KEY_SHOP_NAME = "KEY_shop_name"
    private var INVALID_TOKEN = "-1"

    private fun getSharePreferences() = Application.instance.getSharedPreferences(Config.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE)

    /**
     * 保存用户ID
     */
    fun saveUserId(id: String) = getSharePreferences().edit().putString(KEY_USER_ID, id).apply()

    /**
     * 获取用户ID
     */
    fun getUserId() = getSharePreferences().getString(KEY_USER_ID, "-1")

    /**
     * 保存用户Token
     */
    fun saveToken(token: String) = getSharePreferences().edit().putString(KEY_TOKEN, token).apply()

    /**
     * 获取用户Token
     */
    fun getToken() = getSharePreferences().getString(KEY_TOKEN, INVALID_TOKEN)!!

    /**
     * 清空用户Token
     */
    fun clearToken() = getSharePreferences().edit().remove(KEY_TOKEN).apply()

    /**
     * 检查用户是否登录
     */
    fun checkLogin() = INVALID_TOKEN != getToken()

    /**
     * 保存用户Sid
     */
    fun saveSid(sid: String) = getSharePreferences().edit().putString(KEY_SID, sid).apply()

    /**
     * 获取用户Sid
     */
    fun getSid() = getSharePreferences().getString(KEY_SID, "-1")!!

    /**
     * 保存用户ShopName
     */
    fun saveShopName(shopName: String) = getSharePreferences().edit().putString(KEY_SHOP_NAME, shopName).apply()

    /**
     * 获取用户ShopName
     */
    fun getShopName() = getSharePreferences().getString(KEY_SHOP_NAME, "葵花宝店")!!

    /**
     * 获取版本号名称
     *
     * @param context 上下文
     * @return
     */
    fun getVerName(context: Context): String {
        var verName = ""
        try {
            verName = context.packageManager.getPackageInfo(context.packageName, 0).versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
        }

        return verName
    }

    private var authFailureDialog: Dialog? = null

    val isAuthDialogShowing: Boolean
        get() = authFailureDialog?.isShowing ?: false

    /**
     * 进度对话框
     */
    private var mProgressDialog: Dialog? = null

    /**
     * 进度对话框是否在显示
     */
    val isProgressDialogShowing: Boolean
        get() = null != mProgressDialog && mProgressDialog!!.isShowing

    /**
     * 显示进度对话框
     */
    fun showProgressDialog(context: Context?) {
        if (null == mProgressDialog || !mProgressDialog!!.isShowing) {
            mProgressDialog = ProgressDialog(context)
        }
        mProgressDialog!!.show()
    }

    /**
     * 隐藏进度对话框
     */
    fun hideProgressDialog() {
        if (null != mProgressDialog) {
            mProgressDialog!!.dismiss()
            mProgressDialog = null
        }
    }

    fun showAuthFailureDialog(activityContext: Context, msg: String) {
        hideProgressDialog()
        val builder = AlertDialog.Builder(activityContext)
                .setMessage(msg)
                .setPositiveButton(R.string.button_confirm) { _, _ ->
                    activityContext.startActivity(Intent(activityContext, LoginActivity::class.java))
                }
                .setCancelable(false)

        authFailureDialog = builder.create()
        authFailureDialog!!.setOnDismissListener { _ ->
            authFailureDialog = null
            activityContext.startActivity(Intent(activityContext, LoginActivity::class.java))
        }
        authFailureDialog!!.show()
        Handler().postDelayed({
            if (null != authFailureDialog) {
                authFailureDialog!!.dismiss()
            }
        }, 5000)
    }

    fun showNetworkFailure(context: Context, container: ViewGroup, onRefreshListener: View.OnClickListener?, index: Int = -1): View {
        val webViewFailurePage = LayoutInflater.from(context).inflate(R.layout.webview_failure_page, container, false)
        container.addView(webViewFailurePage, index, ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
        if (null != onRefreshListener) {
            webViewFailurePage.setOnClickListener(onRefreshListener)
        }
        return webViewFailurePage
    }
}